<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库单位社会信用代码" prop="outcreditCode">
        <el-input
          v-model="queryParams.outcreditCode"
          placeholder="请输入出库单位社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库单位社会信用代码" prop="increditCode">
        <el-input
          v-model="queryParams.increditCode"
          placeholder="请输入入库单位社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-sync"
          size="mini"
          @click="handleMsfxBusinessSyncReport"
        >同步商业统计报表数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-sync"
          size="mini"
          @click="handleMsfxTerminalSyncReport"
        >同步终端统计报表数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-sync"
          size="mini"
          @click="handleMsfxOutboundSupplementSyncReport"
        >同步出库补充统计数据
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-sync"
          size="mini"
          @click="handleMsfxBusinessSyncHby"
        >同步商业统计报表数据到HBY
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-sync"
          size="mini"
          @click="handleMsfxTerminalSyncHby"
        >同步终端统计报表数据到HBY
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['hbs:statisticsHbs:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['hbs:statisticsHbs:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statisticsHbsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键ID" align="center" prop="id"/>
      <el-table-column label="出库单位社会信用代码" align="center" prop="outcreditCode"/>
      <el-table-column label="出库数量" align="center" prop="outboundQuantity"/>
      <el-table-column label="入库单位社会信用代码" align="center" prop="increditCode"/>
      <el-table-column label="入库数量" align="center" prop="inboundQuantity"/>
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d} {hh}:{mm}:{ss}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d} {hh}:{mm}:{ss}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {
  listStatisticsHbs,
  getStatisticsHbs,
  getMsfxOutboundSupplementStatistics,
  getMsfxBusinessStatisticsReport,
  getMsfxTerminalStatisticsReport, getMsfxBusinessStatisticsHby, getMsfxTerminalStatisticsHby
} from "@/api/hbs/statisticsHbs"

export default {
  name: "StatisticsHbs",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 码上放心-终端统计表格数据
      statisticsHbsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        creditCode: null,
        receiverName: null,
        inboundQuantity: null,
        statDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        statDate: [
          {required: true, message: "统计日期不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询码上放心-终端统计列表 */
    getList() {
      this.loading = true
      listStatisticsHbs(this.queryParams).then(response => {
        this.statisticsHbsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        creditCode: null,
        receiverName: null,
        inboundQuantity: null,
        statDate: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.creditCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /**
     * 同步商业统计报表数据 - 新增的方法
     */
    handleMsfxBusinessSyncReport() {
      // 开启加载状态
      this.loading = true;
      this.$modal.confirm('是否确认同步商业统计报表数据？').then(() => {
        return getMsfxBusinessStatisticsReport();
      }).then((response) => {
        console.log('API返回:', response);
        this.getList();  // 刷新列表
        this.$modal.msgSuccess(response.msg || "同步成功");
      }).catch((error) => {
        // 只处理真正的错误，忽略用户取消操作
        if (error && error.name !== 'Cancel') {
          console.error('同步失败:', error);
          this.$modal.msgError("同步失败");
        }
      }).finally(() => {
        // 无论成功或失败，都关闭加载状态
        this.loading = false;
      });
    },

    /**
     * 同步终端统计报表数据 - 新增的方法
     */
    handleMsfxTerminalSyncReport() {
      // 开启加载状态
      this.loading = true;
      this.$modal.confirm('是否确认同步终端统计报表数据？').then(() => {
        return getMsfxTerminalStatisticsReport();
      }).then((response) => {
        console.log('API返回:', response);
        this.getList();  // 刷新列表
        this.$modal.msgSuccess(response.msg || "同步成功");
      }).catch((error) => {
        // 只处理真正的错误，忽略用户取消操作
        if (error && error.name !== 'Cancel') {
          console.error('同步失败:', error);
          this.$modal.msgError("同步失败");
        }
      }).finally(() => {
        // 无论成功或失败，都关闭加载状态
        this.loading = false;
      });
    },

    /**
     * 同步出库补充统计数据 - 新增的方法
     */
    handleMsfxOutboundSupplementSyncReport() {
      // 开启加载状态
      this.loading = true;
      this.$modal.confirm('是否确认同步出库补充统计数据？').then(() => {
        return getMsfxOutboundSupplementStatistics();
      }).then((response) => {
        console.log('API返回:', response);
        this.getList();  // 刷新列表
        this.$modal.msgSuccess(response.msg || "同步成功");
      }).catch((error) => {
        // 只处理真正的错误，忽略用户取消操作
        if (error && error.name !== 'Cancel') {
          console.error('同步失败:', error);
          this.$modal.msgError("同步失败");
        }
      }).finally(() => {
        // 无论成功或失败，都关闭加载状态
        this.loading = false;
      });
    },

    /**
     * 同步商业统计报表数据到伙伴云 - 新增的方法
     */
    handleMsfxBusinessSyncHby() {
      // 开启加载状态
      this.loading = true;
      this.$modal.confirm('是否确认同步伙伴云商业统计报表数据？').then(() => {
        return getMsfxBusinessStatisticsHby();
      }).then((response) => {
        console.log('API返回:', response);
        this.getList();  // 刷新列表
        this.$modal.msgSuccess(response.msg || "同步成功");
      }).catch((error) => {
        // 只处理真正的错误，忽略用户取消操作
        if (error && error.name !== 'Cancel') {
          console.error('同步失败:', error);
          this.$modal.msgError("同步失败");
        }
      }).finally(() => {
        // 无论成功或失败，都关闭加载状态
        this.loading = false;
      });
    },
    /**
     * 同步终端统计报表数据到伙伴云 - 新增的方法
     */
    handleMsfxTerminalSyncHby() {
      // 显示加载状态
      this.loading = true;

      this.$modal.confirm('是否确认同步伙伴云终端统计报表数据？').then(() => {
        return getMsfxTerminalStatisticsHby();
      }).then((response) => {
        console.log('API返回:', response);
        this.getList();  // 刷新列表
        this.$modal.msgSuccess(response.msg || "同步成功");
      }).catch((error) => {
        // 只处理真正的错误，忽略用户取消操作
        if (error && error.name !== 'Cancel') {
          console.error('同步失败:', error);
          this.$modal.msgError("同步失败");
        }
      }).finally(() => {
        // 无论成功或失败，都关闭加载状态
        this.loading = false;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.creditCode != null) {
            updateStatisticsHbs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const creditCodes = row.creditCode || this.ids
      this.$modal.confirm('是否确认删除码上放心-终端统计编号为"' + creditCodes + '"的数据项？').then(function () {
        return delStatisticsHbs(creditCodes)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hbs/statisticsHbs/export', {
        ...this.queryParams
      }, `statisticsHbs_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
